package kr.co.graviti.adm.bbs.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import kr.co.graviti.adm.common.dao.AdmCommonDAO;
import kr.co.graviti.common.action.BaseFuncAction;
import kr.co.graviti.common.util.CommonUtil;

public class AdmBbsDeleteProc extends BaseFuncAction{
	
	private static final Logger _log = Logger.getLogger(AdmBbsDeleteProc.class);
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		
		HashMap req = null;
		HashMap bbsSort = new HashMap();
		req = CommonUtil.getRequest(request);
		String ibatisNm = "admBbs";
		AdmCommonDAO dao = new AdmCommonDAO();
		
		/* 게시판 기본정보 */
		int bbsCnt = dao.getIntegerDAO(req, ibatisNm+".selectBbsSortCount");
		if(bbsCnt > 0){
			bbsSort = dao.getHashMapDAO(req, ibatisNm+".selectBbsSort");
			req.put("board_cd", bbsSort.get("BOARD_CD"));
		}else{
			String alertMsg = CommonUtil.printAlertBack("잘못된 페이지 정보 입니다..");	
			request.setAttribute("alertMsg", alertMsg);
		}
		
		HashMap readMap = dao.getHashMapDAO(req, ibatisNm + ".selectBbsView");
		String board_idx = CommonUtil.strNullChk(readMap.get("BOARD_IDX").toString(),"0");
		dao.setDeleteDAO(board_idx, ibatisNm + ".commBoard_delete");
		
		readMap.put("attch_file_idx", CommonUtil.strNullChk(readMap.get("ATTCH_FILE_IDX").toString()));
		List filelist = dao.getArrayListDAO(readMap, ibatisNm+ ".selectAttFileInfo");
		
		HashMap fileDelMap = new HashMap();
		fileDelMap.put("attch_file_idx", readMap.get("ATTCH_FILE_IDX").toString() );
		
		
		for(int i=0;i<filelist.size();i++){
			HashMap fileinfo = new HashMap();
			fileinfo = (HashMap)filelist.get(i);
			
			System.out.println("파일경로 ==>"+ BBS_DEFAULT_PATH + req.get("board_type") + "/" + CommonUtil.strNullChk(fileinfo.get("CHG_FILENM").toString(),"") );
			File f = new File(BBS_DEFAULT_PATH + readMap.get("BOARD_TYPE") + "\\" + CommonUtil.strNullChk(fileinfo.get("CHG_FILENM").toString(),""));
			if(f.exists()) f.delete();
			fileDelMap.put("attch_file_info_idx", CommonUtil.strNullChk(fileinfo.get("ATTCH_FILE_INFO_IDX").toString(),"0"));
			// 첨부파일 삭제 
			dao.setDeleteDAO(fileDelMap, ibatisNm+".fileDelete");
			
		}
		request.setAttribute("req", req);
		return mapping.findForward("success");
	}
}
